<div class="layui-fluid" id="userinfo">

    <div class="layui-row">
        <div class="layui-card ">
            <div class="mybox">
                <form class="layui-form" action="" id="formUser" lay-filter="formUser">
                    <input type="hidden" name="id">
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label">账号:</label>
                        <div class="layui-input-block">
                            <input type="text" name="username" lay-verify="reqired" class="layui-input" disabled="disabled">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label">姓名:</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" lay-verify="reqired" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label">性别:</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="male" title="男"/>
                            <input type="radio" name="sex" value="female" title="女"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label">手机号:</label>
                        <div class="layui-input-block">
                            <input type="text" name="phone" lay-verify="reqired" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label">身份证号:</label>
                        <div class="layui-input-block">
                            <input type="text" name="idcard" lay-verify="reqired" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label">家庭住址:</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" lay-verify="reqired" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-form-item text-right">
                        <button class="layui-btn layui-btn-primary" type="button" id="quxiao">取消</button>
                        <button class="layui-btn" lay-filter="submitUserInfo" lay-submit>保存</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="layui-row" style="margin-top: 20px">
        <div class="layui-card " style="padding:10px 0">
            <div class="mybox">
                <form class="layui-form" action="" id="formpsw">
                    <input type="hidden" name="id">
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label">新密码:</label>
                        <div class="layui-input-block">
                            <input type="password" name="password" lay-verify="reqired" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label">二次新密码:</label>
                        <div class="layui-input-block">
                            <input type="password" name="password1" lay-verify="reqired" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item text-right">
                        <button class="layui-btn" lay-filter="submitPwd" lay-submit>修改密码</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!--css-->
<style>
    #userinfo .mybox{
        padding: 40px;
        width: 600px;
    }
</style>


<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'admin','config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var config = layui.config;
        //数据初始化
        var inituser = function() {
            admin.req("/api/user/info", {}, function (res) {
                if (res.status == 200) {
                    form.val("formUser", res.data)
                } else {
                    layer.msg(res.msg, {icon: 2})
                }
            }, "get")
        }
        inituser()

        //取消
        $("#quxiao").click(function(){
            inituser()
        })
        //修改
        form.on('submit(submitUserInfo)',function(data){
            console.log(data)
            admin.req("/api/user/info",data.field,function (res) {
                if (res.status == 200) {
                    form.val("formUser", res.data)
                    layer.msg(res.msg,{icon:1})
                } else {
                    layer.msg(res.msg, {icon: 2})
                }
            },"post")
            return false
        })

        //修改密码
        form.on('submit(submitPwd)',function(data){
            console.log(data)
            if(data.field.password != data.field.password1){
                layer.open({
                    type:0,
                    content:"两次密码不一致",

                })
                return false
            }
            admin.req('api/user/info',{"password":data.field.password},function(res){
                if(res.status==200){
                    layer.alert("修改成功，请重新登录",{icon:1,title:"提示"},function(index){
                        layer.close(index);
                        config.removeToken();
                        location.reload();
                    })

                }else{
                    layer.msg(res.msg,{icon:2})
                }
                form.val("formpsw",{})
            },"post")
            return false
        })

    });
</script>
